package com.cyy.springbootinit.service.impl;
import java.util.Date;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.springbootinit.common.ErrorCode;
import com.cyy.springbootinit.constant.CommonConstant;
import com.cyy.springbootinit.exception.ThrowUtils;
import com.cyy.springbootinit.mapper.ReadingHistoryMapper;
import com.cyy.springbootinit.model.dto.readhistory.ReadingHistoryQueryRequest;
import com.cyy.springbootinit.model.entity.Dormitory;
import com.cyy.springbootinit.model.entity.ReadingHistory;
import com.cyy.springbootinit.model.entity.User;
import com.cyy.springbootinit.model.vo.ReadingHistoryVO;
import com.cyy.springbootinit.model.vo.UserVO;
import com.cyy.springbootinit.service.ReadingHistoryService;
import com.cyy.springbootinit.service.UserService;
import com.cyy.springbootinit.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 抄表历史表服务实现
 *
 * @author 程序员cyy</a>
 * @from cyy</a>
 */
@Service
@Slf4j
public class ReadingHistoryServiceImpl extends ServiceImpl<ReadingHistoryMapper, ReadingHistory> implements ReadingHistoryService {

    @Resource
    private UserService userService;

    @Resource ReadingHistoryMapper readingHistoryMapper;
    /**
     * 校验数据
     *
     * @param readingHistory
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validReadingHistory(ReadingHistory readingHistory, boolean add) {

        ThrowUtils.throwIf(readingHistory == null, ErrorCode.PARAMS_ERROR);
        // 创建数据时，参数不能为空
        Integer historyId = readingHistory.getHistoryId();
        Integer equipmentId = readingHistory.getEquipmentId();
        Date readingDate = readingHistory.getReadingDate();
        Double readingValue = readingHistory.getReadingValue();
        String remarks = readingHistory.getRemarks();
        if (add) {
            // todo 补充校验规则
            ThrowUtils.throwIf(equipmentId==null, ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(readingValue==null, ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则
    }

    /**
     * 获取查询条件
     *
     * @param readingHistoryQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<ReadingHistory> getQueryWrapper(ReadingHistoryQueryRequest readingHistoryQueryRequest) {
        QueryWrapper<ReadingHistory> queryWrapper = new QueryWrapper<>();
        if (readingHistoryQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Integer historyId = readingHistoryQueryRequest.getHistoryId();
        Integer equipmentId = readingHistoryQueryRequest.getEquipmentId();
        Date readingDate = readingHistoryQueryRequest.getReadingDate();
        Double readingValue = readingHistoryQueryRequest.getReadingValue();
        String remarks = readingHistoryQueryRequest.getRemarks();
        String searchText = readingHistoryQueryRequest.getSearchText();
        String sortField = readingHistoryQueryRequest.getSortField();
        String sortOrder = readingHistoryQueryRequest.getSortOrder();
        // todo 补充需要的查询条件
        // 从多字段中搜索
        if (StringUtils.isNotBlank(searchText)) {
            // 需要拼接查询条件
            queryWrapper.and(qw -> qw.like("title", searchText).or().like("content", searchText));
        }
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(remarks), "remarks", remarks);
        // JSON 数组查询
        // 精确查询
        queryWrapper.ne(ObjectUtils.isNotEmpty(historyId), "historyId",historyId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(equipmentId), "equipmentId", equipmentId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(readingValue), "readingValue", readingValue);
        queryWrapper.eq(ObjectUtils.isNotEmpty(readingDate), "readingDate", readingDate);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取抄表历史表封装
     *
     * @param readingHistory
     * @param request
     * @return
     */
    @Override
    public ReadingHistoryVO getReadingHistoryVO(ReadingHistory readingHistory, HttpServletRequest request) {
        // 对象转封装类
        ReadingHistoryVO readingHistoryVO = ReadingHistoryVO.objToVo(readingHistory);
        return readingHistoryVO;
    }

    /**
     * 分页获取抄表历史表封装
     *
     * @param readingHistoryPage
     * @param request
     * @return
     */
    @Override
    public Page<ReadingHistoryVO> getReadingHistoryVOPage(Page<ReadingHistory> readingHistoryPage, HttpServletRequest request) {
        List<ReadingHistory> readingHistoryList = readingHistoryPage.getRecords();
        Page<ReadingHistoryVO> readingHistoryVOPage = new Page<>(readingHistoryPage.getCurrent(), readingHistoryPage.getSize(), readingHistoryPage.getTotal());
        if (CollUtil.isEmpty(readingHistoryList)) {
            return readingHistoryVOPage;
        }
        // 对象列表 => 封装对象列表
        List<ReadingHistoryVO> readingHistoryVOList = readingHistoryList.stream().map(readingHistory -> {
            return ReadingHistoryVO.objToVo(readingHistory);
        }).collect(Collectors.toList());
        readingHistoryVOPage.setRecords(readingHistoryVOList);
        return readingHistoryVOPage;
    }


    @Override
    public List<ReadingHistory> TwoLastData(List<Integer> equipmentIds) {
        return readingHistoryMapper.TwoLastData(equipmentIds);
    }

}
